add_library(CybertronCore STATIC "")

include_directories("${CYBERTRON_PATH_SOURCE}/CoreLibrary/include")
include_directories("${CYBERTRON_PATH_ROOT}/3rdparty/asio")
include_directories("${CYBERTRON_PATH_ROOT}/3rdparty")
include_directories("${CYBERTRON_PATH_SOURCE}/Protocol/Output/cpp")
include_directories("${CYBERTRON_PATH_ROOT}/3rdparty/pugixml-1.9/src")
include_directories("${CYBERTRON_PATH_ROOT}/3rdparty/libb64-1.2/include")
include_directories("${CYBERTRON_PATH_ROOT}/3rdparty/libcurl/include")
include_directories("${CYBERTRON_PATH_ROOT}/3rdparty/lz4/lib")
include_directories("${CYBERTRON_PATH_ROOT}/3rdparty/miniz-2.1.0")
include_directories("${CYBERTRON_PATH_SOURCE}/Logger/LogApi")
IF (WIN32)
	add_definitions(-D_WIN32_WINNT=0x0501)
	add_definitions(-D_CRT_SECURE_NO_WARNINGS)
ENDIF()

add_definitions(-DNOMINMAX)
add_definitions(-DUNICODE)
add_definitions(-D_UNICODE)
add_definitions(-DMODULE_CYBERTRON_CORE)
add_definitions(-DASIO_STANDALONE)

IF (CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(protoc_process ${CYBERTRON_PATH_ROOT}/3rdparty/artifactory/protoc/protoc.exe)
ENDIF()

IF (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(protoc_process ${CYBERTRON_PATH_ROOT}/3rdparty/artifactory/protoc/protoc)
ENDIF()

IF (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(protoc_process ${CYBERTRON_PATH_ROOT}/3rdparty/artifactory/protoc/protoc)
ENDIF()

set(proto_output_path ${CYBERTRON_PATH_SOURCE}/Protocol/Output/cpp)

foreach (proto_path "${CYBERTRON_PATH_SOURCE}/Protocol/Common"
					"${CYBERTRON_PATH_SOURCE}/Protocol/Sensor"
					"${CYBERTRON_PATH_SOURCE}/Protocol/View"
					"${CYBERTRON_PATH_SOURCE}/Protocol/Node"
					"${CYBERTRON_PATH_SOURCE}/Protocol/Traffic")
	get_filename_component(proto_path_name "${proto_path}" NAME_WE)
	file(GLOB proto_files ${proto_path}/*.proto)
	foreach (proto_file ${proto_files})
		get_filename_component(proto_file_name "${proto_file}" NAME_WE)
		set(proto_cc_output "${proto_output_path}/${proto_path_name}/${proto_file_name}.pb.cc")
		set(proto_h_output "${proto_output_path}/${proto_path_name}/${proto_file_name}.pb.h")
		add_custom_command(
		  OUTPUT "${proto_cc_output}"
				 "${proto_h_output}"
		  COMMAND ${protoc_process}
					"--proto_path=${CYBERTRON_PATH_SOURCE}/Protocol"
					"--cpp_out=${CYBERTRON_PATH_SOURCE}/Protocol/Output/cpp"
					${proto_file}
		  DEPENDS ${proto_file}
		  COMMENT "Generate ${proto_cc_output} ${proto_h_output}"
		  VERBATIM )
	endforeach(proto_file)
endforeach (proto_path)

IF (UNIX)
set_property(TARGET CybertronCore PROPERTY POSITION_INDEPENDENT_CODE ON)
ENDIF()

file(GLOB_RECURSE srcs *.h *.hh *i *.hpp *.c *.cc *.cpp)
target_sources(CybertronCore PRIVATE ${srcs})
source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${srcs})

foreach (path ${CYBERTRON_PATH_ROOT}/3rdparty/stduuid)
	file(GLOB_RECURSE srcs ${path}/*.h ${path}/*.hh ${path}/*i ${path}/*.hpp ${path}/*.c ${path}/*.cc ${path}/*.cpp)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(3rdParty/stduuid FILES ${srcs})
endforeach (path)

foreach (path ${CYBERTRON_PATH_ROOT}/3rdparty/libb64-1.2/include)
	file(GLOB_RECURSE srcs ${path}/*.h ${path}/*.hh ${path}/*i ${path}/*.hpp ${path}/*.c ${path}/*.cc ${path}/*.cpp)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(3rdParty/libb64/include FILES ${srcs})
endforeach (path)

foreach (path ${CYBERTRON_PATH_ROOT}/3rdparty/libb64-1.2/src)
	file(GLOB_RECURSE srcs ${path}/*.h ${path}/*.hh ${path}/*i ${path}/*.hpp ${path}/*.c ${path}/*.cc ${path}/*.cpp)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(3rdParty/libb64/src FILES ${srcs})
endforeach (path)

foreach (path ${CYBERTRON_PATH_ROOT}/3rdparty/asio)
	file(GLOB_RECURSE srcs ${path}/*.h ${path}/*.hh ${path}/*i ${path}/*.hpp ${path}/*.c ${path}/*.cc ${path}/*.cpp)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(3rdParty/asio FILES ${srcs})
endforeach (path)

foreach (path ${CYBERTRON_PATH_ROOT}/3rdparty/json)
	file(GLOB_RECURSE srcs ${path}/*.h ${path}/*.hh ${path}/*i ${path}/*.hpp ${path}/*.c ${path}/*.cc ${path}/*.cpp)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(3rdParty/json FILES ${srcs})
endforeach (path)

foreach (path ${CYBERTRON_PATH_ROOT}/3rdparty/miniz-2.1.0)
	file(GLOB_RECURSE srcs ${path}/miniz.c ${path}/miniz.h)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(3rdParty/miniz FILES ${srcs})
endforeach (path)

foreach (path ${CMAKE_CURRENT_LIST_DIR}/../Protocol/Common  ${CMAKE_CURRENT_LIST_DIR}/../Protocol/Output/cpp/Common)
	file(GLOB_RECURSE srcs ${path}/*.pb.h ${path}/*.pb.cc ${path}/*.proto)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(src/protocol/Common FILES ${srcs})
endforeach (path)

foreach (path ${CMAKE_CURRENT_LIST_DIR}/../Protocol/Node  ${CMAKE_CURRENT_LIST_DIR}/../Protocol/Output/cpp/Node)
	file(GLOB_RECURSE srcs ${path}/*.pb.h ${path}/*.pb.cc ${path}/*.proto)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(src/protocol/Node FILES ${srcs})
endforeach (path)

foreach (path ${CMAKE_CURRENT_LIST_DIR}/../Protocol/Sensor  ${CMAKE_CURRENT_LIST_DIR}/../Protocol/Output/cpp/Sensor)
	file(GLOB_RECURSE srcs ${path}/*.pb.h ${path}/*.pb.cc ${path}/*.proto)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(src/protocol/Sensor FILES ${srcs})
endforeach (path)

foreach (path ${CMAKE_CURRENT_LIST_DIR}/../Protocol/View ${CMAKE_CURRENT_LIST_DIR}/../Protocol/Output/cpp/View)
	file(GLOB_RECURSE srcs ${path}/*.pb.h ${path}/*.pb.cc ${path}/*.proto)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(src/protocol/View FILES ${srcs})
endforeach (path)

foreach (path ${CMAKE_CURRENT_LIST_DIR}/../Protocol/Traffic ${CMAKE_CURRENT_LIST_DIR}/../Protocol/Output/cpp/Traffic)
	file(GLOB_RECURSE srcs ${path}/*.pb.h ${path}/*.pb.cc ${path}/*.proto)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(src/protocol/Traffic FILES ${srcs})
endforeach (path)

foreach (path ${CYBERTRON_PATH_ROOT}/3rdparty/lz4/lib)
	file(GLOB_RECURSE srcs ${path}/*.h ${path}/*.hh ${path}/*i ${path}/*.hpp ${path}/*.c ${path}/*.cc ${path}/*.cpp)
	target_sources(CybertronCore PRIVATE ${srcs})
	source_group(3rdParty/lz4 FILES ${srcs})
endforeach (path)

SetDefaultTargetProperties(CybertronCore)

if (WIN32)
target_link_libraries(CybertronCore advapi32
       iphlpapi
       psapi
       shell32
       user32
       userenv
       ws2_32
       crypt32    #for libcurl
       Wldap32    #for libcurl
       Normaliz   #for libcurl
	   pdh # for pdh_util/query.cpp
#       ${CYBERTRON_PATH_ROOT}/3rdparty/libcurl/lib_win_x64/libcurl_a.lib
)
else()
target_link_libraries(CybertronCore pthread)
target_link_libraries(CybertronCore rt)
#target_link_libraries(CybertronCore protobuf -luuid)
#target_link_libraries(CybertronCore ldap lber z) #for libcurl
#target_link_libraries(CybertronCore ${CYBERTRON_PATH_ROOT}/3rdparty/libcurl/lib_linux_x64/libcurl.a)

endif()

IF (WIN32)
	target_link_libraries(CybertronCore debug ${CYBERTRON_PATH_SOURCE}/../Build/build/lib/Debug/CybertronLogApi.lib)
	target_link_libraries(CybertronCore optimized ${CYBERTRON_PATH_SOURCE}/../Build/build/lib/Release/CybertronLogApi.lib)
ELSEIF (UNIX)
	target_link_libraries(CybertronCore debug ${CYBERTRON_PATH_SOURCE}/../Build/build_debug/lib/libCybertronLogApi.a)
	target_link_libraries(CybertronCore optimized ${CYBERTRON_PATH_SOURCE}/../Build/build_release/lib/libCybertronLogApi.a)
ENDIF()
IF (MSVC)
	SET_PROPERTY(TARGET CybertronCore PROPERTY FOLDER "CoreLibrary")
ENDIF()

IF (MSVC)
	get_filename_component(_vs_bin_path "${CMAKE_LINKER}" DIRECTORY)
	#find_program(LIB_EXE NAMES lib.exe)
	add_custom_command(TARGET CybertronCore POST_BUILD
		COMMAND  "${_vs_bin_path}/lib.exe" /out:tmp.lib $<TARGET_FILE:CybertronCore> ${CYBERTRON_PATH_ROOT}/3rdparty/libcurl/lib_win_x64/libcurl_a.lib ${CYBERTRON_PATH_ROOT}/3rdparty/dongle_api/win/Dongle_s.lib
		COMMAND ${CMAKE_COMMAND} -E rename tmp.lib $<TARGET_FILE:CybertronCore>
	)
ELSEIF (UNIX)
	add_custom_command(TARGET CybertronCore POST_BUILD
		COMMAND rm -f *.o
		COMMAND ar x ${CYBERTRON_PATH_ROOT}/3rdparty/libcurl/lib_linux_x64/libcurl.a
		COMMAND rename 's/^/libcurl-/g' *.o
		COMMAND rename 's/\\.o/.otmp/g' *.o

		COMMAND ar x ${CYBERTRON_PATH_ROOT}/3rdparty/dongle_api/linux/libRockeyARM.a
		COMMAND rename 's/^/libRockeyARM-/g' *.o
		COMMAND rename 's/\\.o/.otmp/g' *.o

		COMMAND ar x $<TARGET_FILE:CybertronCore>
		COMMAND rename 's/^/libcore-/g' *.o
		COMMAND rename 's/\\.o/.otmp/g' *.o

		
		COMMAND rename 's/\\.otmp/.o/g' *.otmp
		COMMAND rm -f $<TARGET_FILE:CybertronCore>
		COMMAND ar cru $<TARGET_FILE:CybertronCore> *.o
		COMMAND ranlib $<TARGET_FILE:CybertronCore>
	)
ENDIF()


